package com.atguigu.spzx.manger.controller;

import com.atguigu.spzx.manger.service.ProductService;
import com.atguigu.spzx.model.dto.product.ProductDto;
import com.atguigu.spzx.model.entity.product.Product;
import com.atguigu.spzx.model.vo.common.Result;
import com.atguigu.spzx.model.vo.common.ResultCodeEnum;
import com.github.pagehelper.PageInfo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/admin/product/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /**
     * 商品上下架
     * @param id
     * @param status
     * @return
     */
    @GetMapping("/updateStatus/{id}/{status}")
    public Result updateStatus(@PathVariable Long id ,
                               @PathVariable Integer  status){

        productService.updateStatus(id,status);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     * 商品审核
     * @param id
     * @param auditStatus
     * @return
     */
    @GetMapping("/updateAuditStatus/{id}/{auditStatus}")
    public Result  updateAuditStatus(@PathVariable Long id ,
                                     @PathVariable Integer auditStatus ){


        productService.updateAuditStatus(id,auditStatus);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除商品信息
     * @param id
     * @return
     */
    @DeleteMapping("/deleteById/{id}")
    public Result deleteById(
            @PathVariable Long id ){
        productService.deleteById(id);

        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     * 修改保存
     * @param product
     * @return
     */
    @PutMapping("/updateById")
    public Result updateById(@RequestBody Product product){

        productService.updateById(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }
    /**
     * 根据id查询商品对象
     * @param id
     * @return
     */
    @GetMapping("/getById/{id}")
    public Result<Product> getProductById(@PathVariable Long id){

        Product product=productService.getProductById(id);

        return Result.build(product,ResultCodeEnum.SUCCESS);
    }

    /**
     * 保存商品
     * @param product
     * @return
     */
    @PostMapping("/save")
    public Result save(@RequestBody Product product){
        productService.save(product);
        return Result.build(null,ResultCodeEnum.SUCCESS);
    }

    /**
     * 条件分页查询商品列表
     * @param pageNum
     * @param pageSize
     * @param productDto
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageInfo<Product>> findByPage(@PathVariable Integer pageNum,
                                                @PathVariable Integer pageSize,
                                                ProductDto productDto){

        PageInfo<Product> productPageInfo=productService.findByPage(pageNum,pageSize,productDto);


        return Result.build(productPageInfo, ResultCodeEnum.SUCCESS);
    }
}
